/*
 * Powered By [rapid-framework]
 * Web Site: http://www.rapid-framework.org.cn
 * Google Code: http://code.google.com/p/rapid-framework/
 * Since 2008 - 2012
 */

package com.tech15.pub.service;

import java.io.BufferedReader;
import java.io.Writer;
import java.sql.Clob;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javacommon.base.BaseManager;
import javacommon.base.EntityDao;
import oracle.sql.CLOB;

import org.hibernate.Hibernate;
import org.hibernate.LockMode;
import org.hibernate.lob.SerializableClob;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import cn.org.rapid_framework.page.Page;
import cn.org.rapid_framework.page.PageRequest;
import cn.org.rapid_framework.util.ObjectUtils;

import com.tech15.core.dao.SysUserDao;
import com.tech15.core.model.SysUser;
import com.tech15.pub.dao.PubDataDao;
import com.tech15.pub.dao.PubDqgzDao;
import com.tech15.pub.model.PubData;
import com.tech15.pub.model.PubDqgz;
import com.tech15.pub.vo.query.PubDqgzQuery;

/**
 * @author 胡作正 email:zuozhenghu@sina.com
 * @version 1.0
 * @since 1.0
 */

@Service
@Transactional
public class PubDqgzManager extends BaseManager<PubDqgz,java.lang.String>{

	private PubDqgzDao pubDqgzDao;
	private SysUserDao sysUserDao;
	private PubDataDao pubDataDao;
	/**增加setXXXX()方法,spring就可以通过autowire自动设置对象属性,请注意大小写*/
	public void setPubDqgzDao(PubDqgzDao dao) {
		this.pubDqgzDao = dao;
	}
	public void setSysUserDao(SysUserDao sysUserDao) {
		this.sysUserDao = sysUserDao;
	}
	public void setPubDataDao(PubDataDao pubDataDao) {
		this.pubDataDao = pubDataDao;
	}
	public EntityDao getEntityDao() {
		return this.pubDqgzDao;
	}
	
	public PubDqgz getById(String id){
		PubDqgz pubDqgz = pubDqgzDao.getById(id);
		Clob clob = pubDqgz.getNr();
		try {
			BufferedReader a = new BufferedReader(clob.getCharacterStream()); //以字符流的方式读入BufferedReader 
			String str = "";
			String content = "";
			while ((str = a.readLine()) != null) {
				content = content.concat(str);
			}
			pubDqgz.setContent(content);
		} catch (Exception e) {
			e.printStackTrace();
		}
		if(ObjectUtils.isNotEmpty(pubDqgz.getZt())){
			PubData pd = pubDataDao.getById(pubDqgz.getZt());
			if(pd != null){
				pubDqgz.setDataname(pd.getName());
			}
		}
		if(ObjectUtils.isNotEmpty(pubDqgz.getLb())){
			PubData pd = pubDataDao.getById(pubDqgz.getLb());
			if(pd != null){
				pubDqgz.setCategoryName(pd.getName());
			}
		}
		if(ObjectUtils.isNotEmpty(pubDqgz.getFbr())){
			SysUser user = sysUserDao.getById(pubDqgz.getFbr());
			if(user != null){
				pubDqgz.setRealname(user.getRealname());
			}
		}

		
		return pubDqgz;
	}
	
	@Transactional(readOnly=true)
	public Page findPage(PubDqgzQuery query) {
		return pubDqgzDao.findPage(query);
	}
	
	@Transactional(readOnly=true)
	public Page findByPageRequest(PageRequest pr) {
		return pubDqgzDao.findByPageRequest(pr);
	}
	
	@Transactional(readOnly=true)
	public Map findPageDqgz(PubDqgzQuery query) {
		Map map = new HashMap();
		List<PubData> lbList = pubDataDao.searchByPid(query.getZt());
		if(lbList != null && lbList.size()>0){
			int i=0;
			for(PubData pd : lbList){
				query.setLb(pd.getId());
				if(i == 0){
					Page page = pubDqgzDao.findPage(query);
					map.put("1", page.getResult());
					map.put("3", pd.getName());
					map.put("5", pd.getPid());
					map.put("6", pd.getId());
				}
				
				if(i == 1){
					Page page = pubDqgzDao.findPage(query);
					map.put("2", page.getResult());
					map.put("4", pd.getName());
					map.put("7", pd.getPid());
					map.put("8", pd.getId());
				}
				i++;
			}
		}
		return map;
	}
	
	public void saveInfo(PubDqgz pubDqgz) {
		//pubDqgz.setFbsj(DateTools.getCurrentTime());
		pubDqgz.setNr(Hibernate.createClob(" "));
		pubDqgzDao.save(pubDqgz);
		pubDqgzDao.flush();
		
		try {
			pubDqgzDao.refresh(pubDqgz, LockMode.UPGRADE);
			SerializableClob cb = (SerializableClob) pubDqgz.getNr();
			CLOB clob = (CLOB) cb.getWrappedClob();
			Writer writer = clob.getCharacterOutputStream();
			writer.write(pubDqgz.getContent());
			writer.close();
		} catch (Exception e) {
			e.printStackTrace();
		}
		
	}
	
	public void updateInfo(PubDqgz pubDqgz) {
		
		//pubInfo.setFbsj(DateTools.getCurrentTime());
		pubDqgz.setNr(Hibernate.createClob(" "));
		pubDqgzDao.update(pubDqgz);
		pubDqgzDao.flush();
	
		
		
		try {
			pubDqgzDao.refresh(pubDqgz, LockMode.UPGRADE);
			SerializableClob cb = (SerializableClob) pubDqgz.getNr();
			CLOB clob = (CLOB) cb.getWrappedClob();
			Writer writer = clob.getCharacterOutputStream();
			writer.write(pubDqgz.getContent());
			writer.close();
		} catch (Exception e) {
			e.printStackTrace();
		}
		
		
	}
	
	
	
	
}
